//
//
//
#include "portocolRegister.h"
#include <utf.h>

_LIT8(KInterfaceNo, "interfaceno");

_LIT8(KReqRegisterURL, "http://58.246.187.214/client.php?ac=register");
_LIT(KReqRegisterData, "username=%s&nickname=%s&token=%s");
_LIT(KReqInfoNone, "\"\"");

CRocketRequestRegister::CRocketRequestRegister(MNetworkEventObervser* aObserver) : CRocketRequest(aObserver)
	{
		iUrl.Copy(KReqRegisterURL());
		iNumber = CMSGREGISTER;
	}

CRocketRequestRegister::~CRocketRequestRegister()
	{
		
	}

void CRocketRequestRegister::SetInfo(const TDesC& aName, const TDesC& aNickName, const TDesC& aToken)
{
	iUserName.Zero();
	iNickName.Zero();
	iToken.Zero();
	
	iUserName.Copy(aName);
	iNickName.Copy(aNickName);
	iToken.Copy(aToken);
}

TBool CRocketRequestRegister::Encode()
{
	TBuf<512> buf;
	buf.Format(KReqRegisterData(),
				iUserName.PtrZ(),
				iNickName.PtrZ());
	
	TBuf8<1024> buf8;
	CnvUtfConverter::ConvertFromUnicodeToUtf8(buf8,buf);
	this->iData = buf8.Alloc();
	return ETrue;
}

CRocketResponseRegister::CRocketResponseRegister()
	{
	
	}

CRocketResponseRegister::~CRocketResponseRegister()
	{
	
	}

void CRocketResponseRegister::OnEndElementL(const Xml::RTagInfo &aElement, 
										TInt aErrorCode)
{
	
}

// XML tags
_LIT8(KRegisterTag, "register");
_LIT8(KRetTag, "ret");

void CRocketResponseRegister::OnStartElementL(const Xml::RTagInfo& aElement,
						const Xml::RAttributeArray& aAttributes,
						TInt aErrorCode)
{
	if(aElement.LocalName().DesC() == KInterfaceNo)
	{
		
	}
	else if(aElement.LocalName().DesC() == KRegisterTag)
	{
		for(TInt nIndex = 0; nIndex < aAttributes.Count(); nIndex ++)
		{
			//Xml::RAttribute attr = aAttributes.At(nIndex);
			if(0 == aAttributes[nIndex].Attribute().LocalName().DesC().Compare(KRetTag))
			{
				TLex8 aLex(aAttributes[nIndex].Value().DesC());
				aLex.Val(iReturnNumber);
			}
		}
	}
}
